#!C:/strawberry/perl/bin/perl.exe

use strict;
use warnings;

use CGI qw(:standard);
use RPC::XML;
use RPC::XML::Client;
use Data::Dumper;
use XML::Simple;
use XML::eXistDB::RPC;
use XML::Compile::RPC::Client;



# read the CGI params
my $cgi = CGI->new;
my $eXist = XML::eXistDB::RPC->new( destination=>"http://localhost:8080/exist/xmlrpc", repository=>"/db", user=>"admin", password=>"eaf2julian" ) ;
my $ciudad = $cgi->param("ciudad");
#my $ciudad="Granada";
my $xquery = 'doc("/placeanddate/markers.xml")/marcadores/marcador' ;
my ($rc1, $set) = $eXist->executeQuery($xquery) ;
my ($rc2, $count) = $eXist->numberOfResults($set) ;
my ($rc3, @data) = $eXist->retrieveResults($set) ;
$eXist->releaseResultSet($set) ;


#print Dumper "------------------";
#print Dumper @data;
#print Dumper $count;
#print Dumper "------------------";

my $lendata=$data[0]{'hitCount'};
#print Dumper $lendata;
print $cgi->header(-type => "application/json", -charset => "utf-8");
my $json="";

if($lendata==0){
	$json=qq{{"hits":"$lendata"}};
}
elsif($lendata==1){
	$json= "{".qq{"hits":"$lendata","marcadores":[};
        my $ciudad = $data[0]{'marcador'}{'ciudad'};
	my $xPosition = $data[0]{'marcador'}{'x'};
	my $yPosition = $data[0]{'marcador'}{'y'};
	my $direccion = $data[0]{'marcador'}{'direccion'};
	my $fecha = $data[0]{'marcador'}{'fecha'};
	my $hora = $data[0]{'marcador'}{'hora'};
	my $comment = $data[0]{'marcador'}{'comentario'};
	my $tipo = $data[0]{'marcador'}{'tipo'};
        my $autor = $data[0]{'marcador'}{'autor'};
        my $id = $data[0]{'marcador'}{'id'};
	$json = $json.qq{{"ciudad" : "$ciudad","xPosition" : "$xPosition","yPosition" : "$yPosition","tipo" : "$tipo","comment" : "$comment","fecha" : "$fecha","direccion" : "$direccion","hora" : "$hora","autor" : "$autor","id" : "$id"}};
	$json=$json."]}";
}
else
{
	$json= "{".qq{"hits":"$lendata","marcadores":[};
	while ($lendata>0) {
                my $ciudad = $data[0]{'marcador'}[$lendata-1]{'ciudad'};
		my $xPosition = $data[0]{'marcador'}[$lendata-1]{'x'};
		my $yPosition = $data[0]{'marcador'}[$lendata-1]{'y'};
		my $direccion = $data[0]{'marcador'}[$lendata-1]{'direccion'};
		my $fecha = $data[0]{'marcador'}[$lendata-1]{'fecha'};
		my $hora = $data[0]{'marcador'}[$lendata-1]{'hora'};
		my $comment = $data[0]{'marcador'}[$lendata-1]{'comentario'};
		my $tipo = $data[0]{'marcador'}[$lendata-1]{'tipo'};
                my $autor = $data[0]{'marcador'}[$lendata-1]{'autor'};
                my $id = $data[0]{'marcador'}[$lendata-1]{'id'};
		# create a JSON string according to the database result
		$json = $json.qq{{"ciudad" : "$ciudad","xPosition" : "$xPosition","yPosition" : "$yPosition","tipo" : "$tipo","comment" : "$comment","fecha" : "$fecha","direccion" : "$direccion","hora" : "$hora","autor" : "$autor","id" : "$id"}};
		# return JSON string
		if ($lendata-1>0){
			$json=$json.",";
		}
		$lendata--;
	}
	$json=$json."]}";
}
print $json;


